package com.xiaoming.service;

import com.xiaoming.Homework3Bootstrap;
import com.xiaoming.entity.DatabaseConfig;
import com.xiaoming.entity.User;
import com.zaxxer.hikari.HikariDataSource;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.ArrayList;
import java.util.List;

@Service
public class UserServiceImpl {

    public List<User> findAll() throws SQLException {
        Connection connection = Homework3Bootstrap.dataSource.getConnection();
        PreparedStatement preparedStatement = connection.prepareStatement("select * from users");
        ResultSet resultSet = preparedStatement.executeQuery();
        List<User> users = new ArrayList<>();
        while (resultSet.next()){
            User user = new User();
            user.setId(resultSet.getString("id"));
            user.setName(resultSet.getString("name"));
            user.setScore(resultSet.getInt("score"));
            user.setStatus(resultSet.getString("status"));
            user.setAge(resultSet.getInt("age"));
            users.add(user);
        }
        return users;
    }
}
